using Engine.Graphics.VertexStructures;
using SharpDX;
using System.Collections.Generic;
using System.Diagnostics;

namespace Engine.Shape
{
    public class Triangulate
    {
        #region Public Methods

        public static void TriangulateBorder(out List<int> shapeIndices, out List<Vector3> shapeVertices, Border border)
        {
            shapeIndices = new List<int>();
            shapeVertices = new List<Vector3>();

            //Point[] m_aPolygon = new Point[7];

            //  m_aPolygon[0] = new Point(-500, -700);
            //  m_aPolygon[1] = new Point(-1800, -2800);
            //  m_aPolygon[2] = new Point(-1400, -1400);

            //  m_aPolygon[3] = new Point(-1500, -850);
            //  m_aPolygon[4] = new Point(-1900, -500);
            //  m_aPolygon[5] = new Point(-2600, -200);
            //  m_aPolygon[6] = new Point(-500, -700);

            int nVertices = 0;// m_aPolygon.Length;

            CPoint2D[] vertices = new CPoint2D[nVertices];

            //positions.Reverse();

            nVertices = border.GetNumberOfPositions();

            vertices = new CPoint2D[nVertices];

            for (int i = 0; i < vertices.Length; i++)
            {
                vertices[i] = new CPoint2D(border.GetPositionAt(i).X,
                    border.GetPositionAt(i).Z, border.GetPositionAt(i).Y);
            }

            if (nVertices > 0)
            {
                CPolygonShape cutPolygon = new CPolygonShape(vertices);
                cutPolygon.CutEar();

                for (int p = 0; p < cutPolygon.NumberOfPolygons; p++)
                {
                    CPoint2D[] vertice = cutPolygon.Polygons(p);
                    if (vertice.Length == 3)
                        for (int i = 0; i < vertice.Length; i++)
                        {
                            Vector3 vert = Vector3.Zero;
                            vert.X = vertice[i].X;
                            vert.Y = vertice[i].Z;
                            vert.Z = vertice[i].Y;
                            shapeVertices.Add(vert);
                        }
                }
            }
        }

        public static void TriangulateShape(out List<MyOwnVertex.MyPositionNormalTextured> shapeVertices, Shapes shapes)
        {
            shapeVertices = new List<MyOwnVertex.MyPositionNormalTextured>();
            int nVertices = 0;
            CPoint2D[] vertices = new CPoint2D[nVertices];
            Border[] borders = shapes.GetBorders();
            for (int b = 0; b < borders.Length; b++)
            {
                Border border = new Border();

                List<Vector3> positions = new List<Vector3>(borders[b].GetPositions());
                positions.Reverse();
                positions.RemoveAt(0);
                //for (int i = 0; i < borders[b].positions.Count - 1; i++)
                //{
                //    positions.Add(borders[b].positions[i]);
                //}
                border = new Border(positions);
                nVertices = border.GetNumberOfPositions();

                vertices = new CPoint2D[nVertices];

                for (int i = 0; i < vertices.Length; i++)
                {
                    vertices[i] = new CPoint2D(border.GetPositionAt(i).X,
                                               border.GetPositionAt(i).Z,
                                               border.GetPositionAt(i).Y);
                }

                if (nVertices >= 3)
                {
                    CPolygonShape cutPolygon = new CPolygonShape(vertices);
                    cutPolygon.CutEar();

                    MyOwnVertex.MyPositionNormalTextured vertex = new MyOwnVertex.MyPositionNormalTextured(
                        // Position
                        Vector3.Zero,
                        // Normal
                        Vector3.Zero,
                        // Texture coordinates #1
                        0.0f, 0.0f,
                        // Texture coordinates #2
                        0.0f, 0.0f,
                        // Diffuse Color
                        new Color4(1, 1, 0, 0),
                        // Vertex blend color
                        new Color4(0, 0, 0, 0));

                    Vector3 vert = Vector3.Zero;

                    for (int p = 0; p < cutPolygon.NumberOfPolygons; p++)
                    {
                        CPoint2D[] vertice = cutPolygon.Polygons(p);

                        for (int i = 0; i < vertice.Length; i++)
                        {
                            vert.X = vertice[i].X;
                            vert.Y = vertice[i].Z;
                            vert.Z = vertice[i].Y;
                            vertex.Position = vert;
                            shapeVertices.Add(vertex);
                        }
                    }
                }
                else
                {
                    Debug.WriteLine("Error during Triangulation. Vertices are below 3");
                }
            }
        }

        public static void TriangulateShape(out List<int> shapeIndices, out List<Vector3> shapeVertices, Shapes shapes)
        {
            shapeIndices = new List<int>();
            shapeVertices = new List<Vector3>();

            //Point[] m_aPolygon = new Point[7];

            //  m_aPolygon[0] = new Point(-500, -700);
            //  m_aPolygon[1] = new Point(-1800, -2800);
            //  m_aPolygon[2] = new Point(-1400, -1400);

            //  m_aPolygon[3] = new Point(-1500, -850);
            //  m_aPolygon[4] = new Point(-1900, -500);
            //  m_aPolygon[5] = new Point(-2600, -200);
            //  m_aPolygon[6] = new Point(-500, -700);

            int nVertices = 0;// m_aPolygon.Length;

            CPoint2D[] vertices = new CPoint2D[nVertices];
            Border[] borders = shapes.GetBorders();
            for (int b = 0; b < borders.Length; b++)
            {
                Border border = new Border();

                List<Vector3> positions = new List<Vector3>();
                for (int i = 0; i < borders[b].GetNumberOfPositions() - 1; i++)
                {
                    positions.Add(borders[b].GetPositionAt(i));
                }
                //positions.Reverse();

                border = new Border(positions);
                nVertices = border.GetNumberOfPositions();

                vertices = new CPoint2D[nVertices];

                for (int i = 0; i < vertices.Length; i++)
                {
                    vertices[i] = new CPoint2D(border.GetPositionAt(i).X,
                        border.GetPositionAt(i).Z, border.GetPositionAt(i).Y);
                }

                if (nVertices > 0)
                {
                    CPolygonShape cutPolygon = new CPolygonShape(vertices);
                    cutPolygon.CutEar();

                    for (int p = 0; p < cutPolygon.NumberOfPolygons; p++)
                    {
                        CPoint2D[] vertice = cutPolygon.Polygons(p);

                        for (int i = 0; i < vertice.Length; i++)
                        {
                            Vector3 vert = Vector3.Zero;
                            vert.X = vertice[i].X;
                            vert.Y = vertice[i].Z;
                            vert.Z = vertice[i].Y;
                            shapeVertices.Add(vert);
                        }
                    }
                }
            }
        }

        #endregion Public Methods
    }
}